package com.yxy.web.controller.common;

import cn.hutool.core.util.IdUtil;
import com.yxy.common.core.domain.AjaxResult;
import com.yxy.system.service.IFileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/api/file")
public class FileUploadController {

    @Autowired
    private IFileStorageService fileStorageService;

    /**
     * 上传文件到临时区域
     */
    @PostMapping("/upload-temp")
    public AjaxResult uploadTempFile(@RequestParam("file") MultipartFile file) {
        try {
            String fileId = IdUtil.fastSimpleUUID();
            String tempFilePath = fileStorageService.saveToTemp(fileId, file);

            Map<String, Object> result = new HashMap<>();
            result.put("fileId", fileId);
            result.put("tempFilePath", tempFilePath);
            result.put("fileName", file.getOriginalFilename());
            result.put("fileSize", file.getSize());

            return AjaxResult.success("文件上传成功", result);
        } catch (Exception e) {
            return AjaxResult.error("文件上传失败: " + e.getMessage());
        }
    }


    // 多文件上传
    @PostMapping("/upload-temp-multi")
    public AjaxResult uploadTempFiles(@RequestParam("files") MultipartFile[] files) {
        try {
            List<Map<String, Object>> results = new ArrayList<>();
            List<String> errorMessages = new ArrayList<>();

            for (MultipartFile file : files) {
                try {
                    String fileId = IdUtil.fastSimpleUUID();
                    String tempFilePath = fileStorageService.saveToTemp(fileId, file);

                    Map<String, Object> result = new HashMap<>();
                    result.put("fileId", fileId);
                    result.put("tempFilePath", tempFilePath);
                    result.put("fileName", file.getOriginalFilename());
                    result.put("fileSize", file.getSize());
                    result.put("status", "success");

                    results.add(result);
                } catch (Exception e) {
                    Map<String, Object> errorResult = new HashMap<>();
                    errorResult.put("fileName", file.getOriginalFilename());
                    errorResult.put("status", "error");
                    errorResult.put("error", e.getMessage());
                    results.add(errorResult);

                    errorMessages.add(file.getOriginalFilename() + ": " + e.getMessage());
                }
            }

            // 构建响应
            Map<String, Object> response = new HashMap<>();
            response.put("files", results);
            response.put("total", files.length);
            response.put("success", results.stream().filter(r -> "success".equals(r.get("status"))).count());
            response.put("failed", results.stream().filter(r -> "error".equals(r.get("status"))).count());

            if (!errorMessages.isEmpty()) {
                return AjaxResult.success("部分文件上传成功", response);
            } else {
                return AjaxResult.success("所有文件上传成功", response);
            }
        } catch (Exception e) {
            return AjaxResult.error("文件上传失败: " + e.getMessage());
        }
    }

}
